# -*- coding: utf-8 -*-
"""
Program to add uncat template to images without categories at commons.
See imagerecat.py (still working on that one) to add these images to categories.

"""
__version__ = '$Id$'
#
# (C) Multichill 2008-2011
# (C) Pywikipedia bot team, 2008-2012
#
# Distributed under the terms of the MIT license.
#
#

import os, sys, re, codecs
from datetime import datetime
from datetime import timedelta
import wikipedia as pywikibot
import config
import pagegenerators
import query

#Probably unneeded because these are hidden categories. Have to figure it out.
ignoreCategories = [u'Category:CC-BY-SA-3.0',
                    u'Category:GFDL',
                    u'Category:Media for cleanup',
                    u'Category:Media lacking a description',
                    u'Category:Media lacking author information',
                    u'Category:Media lacking a description',
                    u'Category:Self-published work',
                    u'Category:Uploaded with UploadWizard',]

#Dont bother to put the template on a image with one of these templates
skipTemplates = [u'Delete',
                 u'Nocat',
                 u'No license',
                 u'No permission since',
                 u'No source',
                 u'No source since',
                 u'Uncategorized',
                 u'Uncat']

#Ignore the templates in this really long list when looking for relevant categories
ignoreTemplates = [ u'1000Bit',
                    u'1922 cyc',
                    u'2MASS',
                    u'Aa',
                    u'Ab',
                    u'AbuGhraibPic',
                    u'ADRM',
                    u'ADRM2',
                    u'AerialPhotograph-mlitJP',
                    u'Af',
                    u'Agência Brasil',
                    u'AgenciaBrasil',
                    u'AgenciaCamaraBr',
                    u'AgenciaSenadoBr',
                    u'Ak',
                    u'AL2TB',
                    u'Alabordache',
                    u'Alexj2002C220PD',
                    u'Alexj2002InkscapePD',
                    u'Aln',
                    u'Als',
                    u'Am',
                    u'An',
                    u'Ang',
                    u'Anonymous work',
                    u'AnotherAuthor',
                    u'Apache',
                    u'Ar',
                    u'Arc',
                    u'Arn',
                    u'Artistic-2',
                    u'As',
                    u'Ashipilin',
                    u'AskedForInfo',
                    u'Ast',
                    u'AstronautPhoto',
                    u'Attribution',
                    u'Attribution Entomart',
                    u'Attribution-Ubisoft',
                    u'Auroranorthlicense',
                    u'Author-de',
                    u'Autorisation photos aériennes Francis Leroy',
                    u'Autotravel',
                    u'Av',
                    u'Avk',
                    u'Ay',
                    u'Az',
                    u'Ba',
                    u'BadJPEG',
                    u'Bar',
                    u'BArch-License',
                    u'BArch-link',
                    u'Bat-smg',
                    u'Bcl',
                    u'Be',
                    u'Be-tarask',
                    u'Be-x-old',
                    u'BeeldbankVenW',
                    u'Bg',
                    u'Bh',
                    u'Bi',
                    u'Blue Marble',
                    u'Bm',
                    u'Bmz',
                    u'Bn',
                    u'Bo',
                    u'Botev license',
                    u'Botev relicensed',
                    u'BotMoveToCommons',
                    u'Bpy',
                    u'Br',
                    u'Bs',
                    u'BSD',
                    u'BSDwithdisclaimer',
                    u'Byp',
                    u'C0',
                    u'Ca',
                    u'CatedralDeSal',
                    u'Cc',
                    u'CC-AR-Presidency',
                    u'Cc-by',
                    u'Cc-by-1.0',
                    u'Cc-by-1.0-nl',
                    u'Cc-by-2.0',
                    u'Cc-by-2.0-at',
                    u'Cc-by-2.0-be',
                    u'Cc-by-2.0-br',
                    u'Cc-by-2.0-cl',
                    u'Cc-by-2.0-de',
                    u'Cc-by-2.0-es',
                    u'Cc-by-2.0-fr',
                    u'Cc-by-2.0-it',
                    u'Cc-by-2.0-kr',
                    u'Cc-by-2.0-nl',
                    u'Cc-by-2.0-uk',
                    u'Cc-by-2.1-au',
                    u'Cc-by-2.1-es',
                    u'Cc-by-2.1-jp',
                    u'Cc-by-2.5',
                    u'Cc-by-2.5-bg',
                    u'Cc-by-2.5-br',
                    u'Cc-by-2.5-dk',
                    u'Cc-by-2.5-es',
                    u'Cc-by-2.5-in',
                    u'Cc-by-2.5-it',
                    u'Cc-by-2.5-my',
                    u'Cc-by-2.5-nl',
                    u'Cc-by-2.5-pl',
                    u'Cc-by-2.5-se',
                    u'Cc-by-2.5,2.0,1.0',
                    u'Cc-by-3.0',
                    u'Cc-by-3.0-gr',
                    u'Cc-by-3.0-IndiaFM',
                    u'Cc-by-3.0-nl',
                    u'Cc-by-3.0-rs',
                    u'Cc-by-3.0-us',
                    u'Cc-by-3.0,2.5,2.0,1.0',
                    u'Cc-by-all',
                    u'Cc-by-nc-sa-2.0-dual',
                    u'Cc-by-sa',
                    u'Cc-by-sa-1.0',
                    u'Cc-by-sa-1.0-fi',
                    u'Cc-by-sa-1.0-tw',
                    u'Cc-by-sa-2.0',
                    u'Cc-by-sa-2.0-at',
                    u'Cc-by-sa-2.0-au',
                    u'Cc-by-sa-2.0-be',
                    u'Cc-by-sa-2.0-br',
                    u'Cc-by-sa-2.0-ca',
                    u'Cc-by-sa-2.0-cl',
                    u'Cc-by-sa-2.0-de',
                    u'Cc-by-sa-2.0-es',
                    u'Cc-by-sa-2.0-fr',
                    u'Cc-by-sa-2.0-it',
                    u'Cc-by-sa-2.0-kr',
                    u'Cc-by-sa-2.0-nl',
                    u'Cc-by-sa-2.0-tw',
                    u'Cc-by-sa-2.0-uk',
                    u'Cc-by-sa-2.1-au',
                    u'Cc-by-sa-2.1-es',
                    u'Cc-by-sa-2.1-jp',
                    u'Cc-by-sa-2.5',
                    u'CC-BY-SA-2.5',
                    u'Cc-by-sa-2.5-ar',
                    u'Cc-by-sa-2.5-au',
                    u'Cc-by-sa-2.5-bg',
                    u'Cc-by-sa-2.5-br',
                    u'Cc-by-sa-2.5-ca',
                    u'Cc-by-sa-2.5-ch',
                    u'Cc-by-sa-2.5-cl',
                    u'Cc-by-sa-2.5-cn',
                    u'Cc-by-sa-2.5-de',
                    u'Cc-by-sa-2.5-dk',
                    u'Cc-by-sa-2.5-es',
                    u'Cc-by-sa-2.5-hu',
                    u'Cc-by-sa-2.5-in',
                    u'Cc-by-sa-2.5-it',
                    u'Cc-by-sa-2.5-mx',
                    u'Cc-by-sa-2.5-my',
                    u'Cc-by-sa-2.5-nl',
                    u'Cc-by-sa-2.5-pl',
                    u'Cc-by-sa-2.5-pt',
                    u'Cc-by-sa-2.5-se',
                    u'Cc-by-sa-2.5-si',
                    u'Cc-by-sa-2.5-tw',
                    u'Cc-by-sa-2.5,1.0',
                    u'Cc-by-sa-2.5,2.0,1.0',
                    u'Cc-by-sa-2.5,2.0,1.0-no-link',
                    u'Cc-by-sa-3.0',
                    u'Cc-by-sa-3.0-gr',
                    u'Cc-by-sa-3.0-nl',
                    u'Cc-by-sa-3.0-rs',
                    u'Cc-by-sa-3.0-tw',
                    u'Cc-by-sa-3.0-us',
                    u'Cc-by-sa-3.0,2.5,2.0,1.0',
                    u'Cc-by-sa-3.0,2.5,2.0,1.0-no-link',
                    u'Cc-by-sa-all',
                    u'Cc-by-sa-jul',
                    u'CC-LukeFord',
                    u'Cc-pd',
                    u'Cc-sa-1.0',
                    u'Cc-zero',
                    u'Cdo',
                    u'Ce',
                    u'Ceb',
                    u'CeCILL',
                    u'CERN-CMS',
                    u'Ch',
                    u'China-PD',
                    u'Cho',
                    u'Chr',
                    u'Chuck',
                    u'Chy',
                    u'Cityview',
                    u'Cityview2',
                    u'Civertan license',
                    u'Claude',
                    u'CNG',
                    u'Co',
                    u'Coa-Germany-b1945',
                    u'Coloured SVG boxes',
                    u'Common Public License',
                    u'Contact-VP',
                    u'Convert to SVG',
                    u'CopyAttribEmail',
                    u'Copyright by Wikimedia',
                    u'Copyright jalpeyrie',
                    u'Copyrighted free use',
                    u'Copyrighted free use provided that',
                    u'Copyrighted IOC',
                    u'CopyrightedFreeUse',
                    u'CopyrightedFreeUse-Link',
                    u'Cr',
                    u'Created by ForrestSjap',
                    u'Created with Inkscape',
                    u'Creator',
                    u'Credit line',
                    u'Credits-panoramafotos.net',
                    u'Crh',
                    u'Crh-cyrl',
                    u'Croatian currency',
                    u'Cs',
                    u'Csb',
                    u'Cu',
                    u'Cv',
                    u'Cy',
                    u'Cz',
                    u'Da',
                    u'Date',
                    u'De',
                    u'De-cc-by-sa-2.0',
                    u'Debora Cordeiro',
                    u'Diq',
                    u'DMonniaux',
                    u'DMonniaux-DADVSI-20060507',
                    u'Dsb',
                    u'DSL',
                    u'Dum',
                    u'Dv',
                    u'Dz',
                    u'EdwardCurtis',
                    u'Ee',
                    u'EEK banknote',
                    u'EEK coin',
                    u'El',
                    u'Elephants Dream',
                    u'Eml',
                    u'En',
                    u'En icon',
                    u'Eo',
                    u'EPL',
                    u'Erin Silversmith Licence',
                    u'Es',
                    u'EST-Land Board',
                    u'Estremeñu',
                    u'Et',
                    u'Eu',
                    u'EU location',
                    u'Euro coin common face',
                    u'Europe location',
                    u'Expat',
                    u'Ext',
                    u'Fa',
                    u'FAL',
                    u'Fancyfootworklicense',
                    u'Faroe stamps',
                    u'Fcb981c',
                    u'Ff',
                    u'Fi',
                    u'FirefoxWiki',
                    u'Fiu-vro',
                    u'Fj',
                    u'Flag-Germany-b1945',
                    u'Flickr',
                    u'Flickr-change-of-license',
                    u'Flickr-unfree-but',
                    u'Flickrreview',
                    u'Fo',
                    u'FOLP',
                    u'FOP',
                    u'Fr',
                    u'Fr icon',
                    u'Frc',
                    u'Free screenshot',
                    u'Frp',
                    u'Fur',
                    u'FWL',
                    u'Fy',
                    u'Ga',
                    u'Gag',
                    u'Gakk Copyright',
                    u'Gan',
                    u'Gd',
                    u'Geograph',
                    u'GFDL',
                    u'GFDL or cc-by-nc-sa',
                    u'GFDL or cc-by-nc-sa/2.5',
                    u'GFDL-1.2',
                    u'GFDL-1.2-en',
                    u'GFDL-CC-triple',
                    u'GFDL-DD',
                    u'GFDL-en',
                    u'GFDL-GMT',
                    u'GFDL-Institut de Qualitat Agroalimentària',
                    u'GFDL-IS',
                    u'GFDL-it',
                    u'GFDL-ja',
                    u'GFDL-Landsat-Kashmir3d',
                    u'GFDL-OpenGeoDB',
                    u'GFDL-retouched',
                    u'GFDL-Samoborac',
                    u'GFDL-self',
                    u'GFDL-Self',
                    u'GFDL-self-en',
                    u'GFDL-user',
                    u'GFDL-user-als',
                    u'GFDL-user-ar',
                    u'GFDL-user-bat-smg',
                    u'GFDL-user-bs',
                    u'GFDL-user-cs',
                    u'GFDL-user-da',
                    u'GFDL-user-de',
                    u'GFDL-user-el',
                    u'GFDL-user-en',
                    u'GFDL-user-en-no-disclaimers',
                    u'GFDL-user-en-note',
                    u'GFDL-user-en-with-disclaimers',
                    u'GFDL-user-es',
                    u'GFDL-user-fa',
                    u'GFDL-user-fi',
                    u'GFDL-user-fr',
                    u'GFDL-user-gl',
                    u'GFDL-user-he',
                    u'GFDL-user-hi',
                    u'GFDL-user-hu',
                    u'GFDL-user-id',
                    u'GFDL-user-it',
                    u'GFDL-user-ja',
                    u'GFDL-user-ko',
                    u'GFDL-user-lt',
                    u'GFDL-user-nl',
                    u'GFDL-user-nn',
                    u'GFDL-user-no',
                    u'GFDL-user-pl',
                    u'GFDL-user-pt',
                    u'GFDL-user-ru',
                    u'GFDL-user-sk',
                    u'GFDL-user-sq',
                    u'GFDL-user-tr',
                    u'GFDL-user-uk',
                    u'GFDL-user-vi',
                    u'GFDL-user-vls',
                    u'GFDL-user-w',
                    u'GFDL-user-zh',
                    u'Giovannino Copyright',
                    u'Gl',
                    u'Glk',
                    u'Gloumouth1Credit',
                    u'Gn',
                    u'GNOME-icon-theme',
                    u'Got',
                    u'GPL',
                    u'GPLv2 only',
                    u'GPLv3',
                    u'Gr',
                    u'Grays Anatomy plate',
                    u'Gu',
                    u'Gv',
                    u'GWArt',
                    u'GWArtOld',
                    u'GWPDA',
                    u'Ha',
                    u'HAER',
                    u'Hak',
                    u'Halibutt Copyright GFDL',
                    u'Halibutt Copyright Ogg',
                    u'HALS',
                    u'Harry',
                    u'Harry2',
                    u'Haw',
                    u'He',
                    u'Heraldic Badge',
                    u'Hi',
                    u'Hiuppo Copyright GFDL',
                    u'Ho',
                    u'Hr',
                    u'Hsb',
                    u'Ht',
                    u'Hu',
                    u'Hy',
                    u'Hz',
                    u'Ia',
                    u'Ibiblio-Hyperwar.org',
                    u'Icelandic currency',
                    u'Icelandic stamp',
                    u'Id',
                    u'Ie',
                    u'Ig',
                    u'Ii',
                    u'Ik',
                    u'Ike-latn',
                    u'Ilo',
                    u'ImageUpload',
                    u'Img-confirmation',
                    u'INewton',
                    u'Information',
                    u'Information Picswiss',
                    u'Inkscape',
                    u'Insignia',
                    u'Insignia Catalonia',
                    u'Insignia Navarre',
                    u'Io',
                    u'IPPAR',
                    u'Is',
                    u'It',
                    u'It icon',
                    u'Iu',
                    u'Ja',
                    u'Ja2',
                    u'James K. Lindsey permission',
                    u'Jbo',
                    u'Jean-PolGRANDMONTCredit',
                    u'JewishEncyclopedia',
                    u'Joergens.mi licence',
                    u'JPEG version of PNG',
                    u'Jv',
                    u'Ka',
                    u'Kaa',
                    u'Kab',
                    u'Kameno doba',
                    u'Kg',
                    u'Ki',
                    u'Kirkeinfo',
                    u'Kirkenorge',
                    u'Kj',
                    u'Kk',
                    u'Kl',
                    u'Km',
                    u'Kn',
                    u'Ko',
                    u'Kopimi',
                    u'Kr',
                    u'Krj',
                    u'Ks',
                    u'Ksh',
                    u'Ku',
                    u'Kv',
                    u'Kw',
                    u'Ky',
                    u'La',
                    u'LA2-Blitz',
                    u'Lad',
                    u'Lan',
                    u'LarsenCopyright',
                    u'Lb',
                    u'Lbe',
                    u'Lfn',
                    u'Lg',
                    u'LGPL',
                    u'Li',
                    u'LibriVox public domain',
                    u'License',
                    u'Lij',
                    u'LinuxeristCopyright',
                    u'Lld',
                    u'Lmo',
                    u'Ln',
                    u'Lo',
                    u'LOC-image',
                    u'LOC-pchrom',
                    u'LOC-prok',
                    u'LOC-prokc',
                    u'Location',
                    u'Location dec',
                    u'Location dms',
                    u'LocationRequired',
                    u'Lothar1976',
                    u'Loz',
                    u'Ls',
                    u'Lt',
                    u'Lueger',
                    u'Lv',
                    u'LviatourCredit',
                    u'Map of Japan-Shigenobu AOKI',
                    u'Map-Austria-GNu',
                    u'Marine-marchande.net',
                    u'MartinX',
                    u'Master son',
                    u'MdB',
                    u'Mdf',
                    u'Met.no',
                    u'Mg',
                    u'Mh',
                    u'Mi',
                    u'Mildenhall',
                    u'MindInfo',
                    u'MIT',
                    u'Mk',
                    u'Ml',
                    u'MLDoan',
                    u'Mn',
                    u'Mo',
                    u'MoengCredit',
                    u'Money-EU banknote',
                    u'Money-Eu',
                    u'Money-REAL',
                    u'MORS',
                    u'MPL',
                    u'Mr',
                    u'Ms',
                    u'Mt',
                    u'MTL',
                    u'Museum.ru',
                    u'My',
                    u'Myv',
                    u'Mzn',
                    u'Na',
                    u'Nah',
                    u'Namespace',
                    u'NAMESPACE',
                    u'Nap',
                    u'NAUMANN',
                    u'Nb',
                    u'Nds',
                    u'Nds-nl',
                    u'Ne',
                    u'NetMarine',
                    u'New',
                    u'Newsflash Photo',
                    u'Ng',
                    u'NGC7000',
                    u'NGruev',
                    u'NGW',
                    u'Ngw2',
                    u'Nickshanks-cc-by-2.0-email',
                    u'Njegos.org',
                    u'Nl',
                    u'Nl icon',
                    u'Nn',
                    u'Nn icon',
                    u'No',
                    u'No icon',
                    u'Non',
                    u'Norges Golfforbund',
                    u'Norwegian coat of arms',
                    u'Not-PD-US-URAA',
                    u'Nov',
                    u'Nrbelex Photo',
                    u'Nrbelex Photo 400D',
                    u'Nrm',
                    u'Nso',
                    u'Nv',
                    u'Ny',
                    u'NYC Subway map',
                    u'NYPL-image-full',
                    u'OAL',
                    u'Object location',
                    u'Oc',
                    u'OldOS',
                    u'Olessi Copyright Ogg',
                    u'Om',
                    u'Open Beelden',
                    u'Open Font',
                    u'Openphotoreview',
                    u'Or',
                    u'Originally uploaded',
                    u'Os',
                    u'OsborneFossils',
                    u'OTrondal',
                    u'OTRS',
                    u'Otrs pending',
                    u'OTRS pending',
                    u'Own',
                    u'Own work',
                    u'Pa',
                    u'Pag',
                    u'PAGENAME',
                    u'PAGENAMEE',
                    u'Painting',
                    u'Pam',
                    u'Pap',
                    u'Parlament.ch',
                    u'PAshieldsource',
                    u'PBresseler',
                    u'PCL',
                    u'PD',
                    u'PD Colombia',
                    u'PD patents',
                    u'PD-1923',
                    u'PD-AB-exempt',
                    u'PD-Abdul Hamid',
                    u'PD-AIGA',
                    u'PD-Albania-exempt',
                    u'PD-Algeria',
                    u'PD-AM-exempt',
                    u'PD-ancient-script',
                    u'PD-AR-Anonymous',
                    u'PD-AR-Movie',
                    u'PD-AR-Photo',
                    u'PD-Archivesnormandie',
                    u'PD-Armenia',
                    u'PD-Art',
                    u'PD-art',
                    u'PD-art-life-70',
                    u'PD-art-US',
                    u'PD-Art-YorckProject',
                    u'PD-Australia',
                    u'PD-AustrianGov',
                    u'PD-author',
                    u'PD-AZ-exempt',
                    u'PD-Azerbaijan',
                    u'PD-Bahrain',
                    u'PD-Bain',
                    u'PD-Bangladesh',
                    u'PD-because',
                    u'PD-BH-exempt',
                    u'PD-BnFMandragorePic',
                    u'PD-Brady-Handy',
                    u'PD-Brazil-media',
                    u'PD-BrazilGov',
                    u'PD-Britannica',
                    u'PD-Brockhaus&Efron',
                    u'PD-BulgarianGov',
                    u'PD-BW',
                    u'PD-BY-exempt',
                    u'PD-Canada',
                    u'PD-Canada-creator',
                    u'PD-Canada-Crown',
                    u'PD-Canada-photo',
                    u'PD-Canada-stamp',
                    u'PD-Challenger Report',
                    u'PD-chem',
                    u'PD-Chile',
                    u'PD-China',
                    u'PD-Coa-Finland',
                    u'PD-Coa-Germany',
                    u'PD-Coa-Hungary',
                    u'PD-Coins-Krenzer',
                    u'PD-collective anonymous work',
                    u'PD-copyright holder',
                    u'PD-Croatia',
                    u'PD-CroatiaGov',
                    u'PD-CSIRO',
                    u'PD-Cuba',
                    u'PD-CzechGov',
                    u'PD-DAUI',
                    u'PD-DBZ stamps',
                    u'PD-Demis',
                    u'PD-Denmark50',
                    u'PD-DenmarkEVH',
                    u'PD-Detroit',
                    u'PD-Deutsche Bundespost stamps',
                    u'PD-Edison Records',
                    u'PD-EE-exempt',
                    u'PD-EEA',
                    u'PD-Egypt',
                    u'PD-EstoniaPub',
                    u'PD-EU-no author disclosure',
                    u'PD-Feldman-1905',
                    u'PD-Fiji',
                    u'PD-Finland',
                    u'PD-Finland50',
                    u'PD-FinlandGov',
                    u'PD-flag',
                    u'PD-Flag',
                    u'PD-flag-50',
                    u'PD-Flag-Germany',
                    u'PD-FLGov',
                    u'PD-font',
                    u'PD-FOP-DE',
                    u'PD-Fröléens konversationslexikon',
                    u'PD-GallicaPic',
                    u'PD-GDR stamps',
                    u'PD-GE-exempt',
                    u'PD-Generic',
                    u'PD-German Empire stamps',
                    u'PD-German postmarks',
                    u'PD-German stamps',
                    u'PD-GermanGov',
                    u'PD-Google books',
                    u'PD-Gottscho',
                    u'PD-GreekGov',
                    u'PD-Gutenberg',
                    u'PD-heirs',
                    u'PD-Highsmith',
                    u'PD-HK',
                    u'PD-HK-PR',
                    u'PD-HU-exempt',
                    u'PD-Hubble',
                    u'PD-IDGov',
                    u'PD-IDOld-Art29',
                    u'PD-IDOld-Art30',
                    u'PD-imf.org',
                    u'PD-India',
                    u'PD-ineligible',
                    u'PD-Internationale',
                    u'PD-Iran',
                    u'PD-Iraq',
                    u'PD-Ireland',
                    u'PD-IrishGov',
                    u'PD-Israel',
                    u'PD-Israel-Photo',
                    u'PD-ItalyGov',
                    u'PD-Japan',
                    u'PD-Japan-exempt',
                    u'PD-Japan-film',
                    u'PD-Japan-oldphoto',
                    u'PD-Japan-organization',
                    u'PD-Jordan',
                    u'PD-Jordan-Photo',
                    u'PD-JORF',
                    u'PD-JORF-nor-conso',
                    u'PD-Kenya',
                    u'PD-KG-exempt',
                    u'PD-Kuwait',
                    u'PD-KZ-exempt',
                    u'PD-Lebanon',
                    u'PD-Libya',
                    u'PD-link',
                    u'PD-LOC',
                    u'PD-Look',
                    u'PD-LosAlamos',
                    u'PD-Lozinski',
                    u'PD-LT-exempt',
                    u'PD-LV-exempt',
                    u'PD-MacaoGov',
                    u'PD-magic',
                    u'PD-Malawi',
                    u'PD-Manchukuo-stamps',
                    u'PD-MapLibrary',
                    u'PD-Mauritius',
                    u'PD-MD-exempt',
                    u'PD-Mexico',
                    u'PD-Mexico-NIP',
                    u'PD-Meyers',
                    u'PD-Meyers-5th-edition',
                    u'PD-Meyers-6th-edition',
                    u'PD-MNGov',
                    u'PD-MO',
                    u'PD-money-Romania',
                    u'PD-Morocco',
                    u'PD-Namibia',
                    u'PD-National Photo Company',
                    u'PD-Nigeria',
                    u'PD-NL-gemeentevlag',
                    u'PD-NL-gemeentewapen',
                    u'PD-NL-Gov',
                    u'PD-NL-verkeersbord',
                    u'PD-Nordens Flora',
                    u'PD-North Korea',
                    u'PD-Norway50',
                    u'PD-NorwayGov',
                    u'PD-NTBB',
                    u'PD-NZ',
                    u'PD-NZ-50-years',
                    u'PD-old',
                    u'Pd-old',
                    u'PD-Old',
                    u'PD-old-100',
                    u'PD-old-50',
                    u'PD-old-70',
                    u'PD-old-75',
                    u'PD-old-80',
                    u'PD-old-Edition',
                    u'PD-Oman',
                    u'PD-OpenClipart',
                    u'PD-Orgelbrand',
                    u'PD-Ornament1898',
                    u'PD-Pakistan',
                    u'PD-PCL-portraits',
                    u'PD-PDFnet',
                    u'PD-PDphoto.org',
                    u'PD-Peru',
                    u'PD-Philippines',
                    u'PD-PhilippinesPubDoc',
                    u'PD-PMR-exempt',
                    u'PD-Polish',
                    u'PD-Polishsymbol',
                    u'PD-PRC-exempt',
                    u'PD-PT',
                    u'PD-retouched-user',
                    u'PD-retouched-user-w',
                    u'PD-RO-exempt',
                    u'PD-ROC-exempt',
                    u'PD-ROC-Traffic Indicating Lines',
                    u'PD-ROC-Traffic Signs',
                    u'PD-RU-exempt',
                    u'PD-RusEmpire',
                    u'PD-Russia',
                    u'PD-Russia-2008',
                    u'PD-SAGov',
                    u'PD-Saudi Arabia',
                    u'PD-SBH',
                    u'PD-scan',
                    u'PD-Scan',
                    u'PD-SCGGov',
                    u'PD-ScottForesman',
                    u'PD-ScottForesman-raw',
                    u'PD-script',
                    u'PD-Seal-Germany',
                    u'PD-Seattle-Neighborhood-Atlas',
                    u'PD-SeaWiFS',
                    u'PD-self',
                    u'PD-self2',
                    u'PD-SerbiaGov',
                    u'PD-Seychelles',
                    u'PD-SFJ',
                    u'PD-shape',
                    u'PD-Sjöfartsverket',
                    u'PD-SlovakGov',
                    u'PD-South Korea',
                    u'PD-South-Africa',
                    u'PD-Soviet-revised',
                    u'PD-SRBGov',
                    u'PD-STFP',
                    u'PD-StVZVO',
                    u'PD-Sudan',
                    u'PD-Suomen',
                    u'PD-Sweden',
                    u'PD-Sweden-1969',
                    u'PD-Sweden-photo',
                    u'PD-Sweden-self',
                    u'PD-Switzerland-official',
                    u'PD-Switzerland-photo',
                    u'PD-Syria',
                    u'PD-Taiwan',
                    u'PD-text',
                    u'PD-textlogo',
                    u'PD-TH-exempt',
                    u'PD-Thailand',
                    u'PD-TJ-exempt',
                    u'PD-TK-exempt',
                    u'PD-TR-Gov',
                    u'PD-Traditional',
                    u'PD-Tunisia',
                    u'PD-UA-exempt',
                    u'PD-UEA',
                    u'PD-Uganda',
                    u'PD-Ugglan',
                    u'PD-UK-known',
                    u'PD-UK-unknown',
                    u'PD-UKGov',
                    u'PD-Ukraine',
                    u'PD-UN',
                    u'PD-United Arab Emirates',
                    u'PD-URAA',
                    u'PD-Uruguay',
                    u'PD-US',
                    u'PD-US-1978-89',
                    u'PD-US-flag',
                    u'PD-US-no notice',
                    u'PD-US-not renewed',
                    u'PD-US-patent',
                    u'PD-US-patent-no notice',
                    u'PD-US-record',
                    u'PD-user',
                    u'PD-User',
                    u'PD-user-als',
                    u'PD-user-ar',
                    u'PD-user-ca',
                    u'PD-user-cs',
                    u'PD-user-da',
                    u'PD-user-de',
                    u'PD-user-en',
                    u'PD-user-fa',
                    u'PD-user-fr',
                    u'PD-user-he',
                    u'PD-user-hi',
                    u'PD-user-hr',
                    u'PD-user-hu',
                    u'PD-user-it',
                    u'PD-user-ja',
                    u'PD-user-lt',
                    u'PD-user-nl',
                    u'PD-user-nn',
                    u'PD-user-no',
                    u'PD-user-pl',
                    u'PD-user-pt',
                    u'PD-user-ro',
                    u'PD-user-ru',
                    u'PD-user-sk',
                    u'PD-user-sl',
                    u'PD-user-th',
                    u'PD-user-uk',
                    u'PD-user-vls',
                    u'PD-user-w',
                    u'PD-user-zh',
                    u'PD-USGov',
                    u'PD-USGov-ARM',
                    u'PD-USGov-Atlas',
                    u'PD-USGov-Award',
                    u'PD-USGov-BLM',
                    u'PD-USGov-CIA',
                    u'PD-USGov-CIA-WF',
                    u'PD-USGov-Congress',
                    u'PD-USGov-Congress-AOC',
                    u'PD-USGov-Congress-Bio',
                    u'PD-USGov-DEA',
                    u'PD-USGov-DHS',
                    u'PD-USGov-DHS-CG',
                    u'PD-USGov-DHS-CGAUX',
                    u'PD-USGov-DOC',
                    u'PD-USGov-DOC-Census',
                    u'PD-USGov-DOE',
                    u'PD-USGov-DOJ',
                    u'PD-USGov-DOL',
                    u'PD-USGov-DOS',
                    u'PD-USGov-DOT',
                    u'PD-USGov-DVA',
                    u'PD-USGov-ED',
                    u'PD-USGov-EPA',
                    u'PD-USGov-FAA',
                    u'PD-USGov-FBI',
                    u'PD-USGov-FDA',
                    u'PD-USGov-Federal Reserve',
                    u'PD-USGov-FEMA',
                    u'PD-USGov-FSA',
                    u'PD-USGov-FWS',
                    u'PD-USGov-HHS',
                    u'PD-USGov-HHS-CDC',
                    u'PD-USGov-HHS-NIH',
                    u'PD-USGov-Interior',
                    u'PD-USGov-Interior-HABS',
                    u'PD-USGov-Interior-MMS',
                    u'PD-USGov-Interior-USBR',
                    u'PD-USGov-Interior-USGS-Minerals',
                    u'PD-USGov-Military',
                    u'PD-USGov-Military award',
                    u'PD-USGov-Military-Air Force',
                    u'PD-USGov-Military-Air Force Auxiliary',
                    u'PD-USGov-Military-Army',
                    u'PD-USGov-Military-Army-USACE',
                    u'PD-USGov-Military-Army-USACMH',
                    u'PD-USGov-Military-Army-USAIOH',
                    u'PD-USGov-Military-Badge',
                    u'PD-USGov-Military-Coast Guard',
                    u'PD-USGov-Military-DVIC',
                    u'PD-USGov-Military-Marines',
                    u'PD-USGov-Military-MDA',
                    u'PD-USGov-Military-National Guard',
                    u'PD-USGov-Military-Navy',
                    u'PD-USGov-Military-NGA',
                    u'PD-USGov-money',
                    u'PD-USGov-MUTCD',
                    u'PD-USGov-NASA',
                    u'PD-USGov-NASA/copyright',
                    u'PD-USGov-NCBI-scienceprimer',
                    u'PD-USGov-NIH',
                    u'PD-USGov-NIST',
                    u'PD-USGov-NOAA',
                    u'PD-USGov-NPS',
                    u'PD-USGov-NRO',
                    u'PD-USGov-NSA',
                    u'PD-USGov-NSF',
                    u'PD-USGov-NTSB',
                    u'PD-USGov-POTUS',
                    u'PD-USGov-State',
                    u'PD-USGov-Treasury',
                    u'PD-USGov-TVA',
                    u'PD-USGov-USAID',
                    u'PD-USGov-USDA',
                    u'PD-USGov-USDA-ARS',
                    u'PD-USGov-USDA-FS',
                    u'PD-USGov-USDA-NAL',
                    u'PD-USGov-USDA-NRCS',
                    u'PD-USGov-USGS',
                    u'PD-USGov-USIA',
                    u'PD-USGov-VOA',
                    u'PD-USGov-WPA',
                    u'PD-USNWR',
                    u'PD-UZ-exempt',
                    u'PD-Van Vechten',
                    u'PD-Vegagerdin',
                    u'PD-Venezuela',
                    u'PD-Vietnam',
                    u'PD-VL-shield',
                    u'PD-Vlaams-gemeentewapen',
                    u'PD-Vlaamse-gemeentevlag',
                    u'PD-VzKat',
                    u'PD-WorldWind',
                    u'PD-Yemen',
                    u'PD-Yugoslavia',
                    u'PD-Zimbabwe',
                    u'Pd',
                    u'Pdc',
                    u'Peregrine981',
                    u'PermissionOTRS',
                    u'PermissionOTRS-ID',
                    u'Personality rights',
                    u'Photo-by-Wojciechowscy-GFDL',
                    u'Photocity.ru',
                    u'PhotoManifs-PP',
                    u'Photos by Bagn Bygdesamling',
                    u'Photos by Kulturhistorisk museum',
                    u'Photos by Stortinget',
                    u'Photos by the Norwegian Museum of Cultural History',
                    u'Pi',
                    u'Picswiss',
                    u'Piratpartiet',
                    u'Pl',
                    u'Pl icon',
                    u'PLoS',
                    u'Pms',
                    u'PNG with JPEG version',
                    u'Pnt',
                    u'Polish coats of arms by Tadeusz Gajl',
                    u'PolishPresidentCopyright',
                    u'PolishSenateCopyright',
                    u'Polishsymbol',
                    u'Politas FZ30',
                    u'Portpictures.nl',
                    u'Pousbeeld',
                    u'Pressefotos Die Gruenen',
                    u'Ps',
                    u'Pt',
                    u'Pt icon',
                    u'Pt-br',
                    u'Qu',
                    u'QualityImage',
                    u'RadioSenadoBr',
                    u'RetouchedPicture',
                    u'Rm',
                    u'Rmy',
                    u'Rn',
                    u'Ro',
                    u'Roa-rup',
                    u'RomanianMCTICopyright',
                    u'RTCNCA License',
                    u'Rtl-lang',
                    u'Ru',
                    u'Rw',
                    u'Sa',
                    u'Sah',
                    u'Sc',
                    u'Scn',
                    u'Sco',
                    u'Sd',
                    u'Sdc',
                    u'Se',
                    u'Seattle Neighborhood Atlas disclaimer',
                    u'Second Life',
                    u'Sei',
                    u'SejmCopyright',
                    u'Self',
                    u'Self2',
                    u'Self2-name',
                    u'Self3',
                    u'Self4',
                    u'Self-GFDL-German',
                    u'SelfSA-GFDL-German',
                    u'SenadoMexico',
                    u'Sergeymila',
                    u'Sg',
                    u'SgiraldoaCredit',
                    u'Sh',
                    u'Shi',
                    u'Si',
                    u'SIB',
                    u'Simple',
                    u'Simple English',
                    u'Sk',
                    u'Sl',
                    u'Sm',
                    u'Sma',
                    u'Sn',
                    u'So',
                    u'Solkoll',
                    u'Solkoll 2D',
                    u'Solkoll 3D',
                    u'South Korean currency',
                    u'SpaceShuttle',
                    u'SPD-Parteivorstand',
                    u'Spui',
                    u'Spuiother',
                    u'Sq',
                    u'Sr',
                    u'Srn',
                    u'Ss',
                    u'St',
                    u'Stan Shebs photo',
                    u'Stationsweb',
                    u'Statistics Netherlands map',
                    u'Stielers Handatlas 1891',
                    u'Stortinget3',
                    u'Stq',
                    u'Stratosphere',
                    u'Su',
                    u'SupersededPNG',
                    u'SupersededSVG',
                    u'Supported by Wikimedia France',
                    u'Sv',
                    u'Svensk porträttgalleri',
                    u'Svg',
                    u'SVG',
                    u'SVG-Map Africa by Slomox',
                    u'Sw',
                    u'Swiss Government Portrait',
                    u'SygnaturaPixela',
                    u'Szl',
                    u'Ta',
                    u'Tango',
                    u'Taxonavigation',
                    u'Tcolphoto',
                    u'Te',
                    u'Texas State Highway',
                    u'Tet',
                    u'Tg',
                    u'Tgl',
                    u'Tintazul',
                    u'Th',
                    u'ThomasBredolCredit2',
                    u'ThomasBredolCredit3',
                    u'Ti',
                    u'Tk',
                    u'Tl',
                    u'Tlx',
                    u'Tn',
                    u'To',
                    u'TomCorserCredit',
                    u'Tpi',
                    u'Tr',
                    u'Trademark',
                    u'Trademarked',
                    u'Ts',
                    u'Tt',
                    u'Tum',
                    u'TVSenadoBr',
                    u'Tw',
                    u'Ty',
                    u'Tyv',
                    u'Udm',
                    u'Ug',
                    u'Uk',
                    u'UN map',
                    u'Ur',
                    u'User:FlickreviewR/reviewed-pass',
                    u'User:Fir0002/20D',
                    u'USDA',
                    u'UWiscCIMSS',
                    u'Uz',
                    u'Ve',
                    u'Vec',
                    u'Vector-Images.com',
                    u'Vector version available',
                    u'Vi',
                    u'Viollet-le-Duc',
                    u'Vl',
                    u'Vls',
                    u'Vo',
                    u'W',
                    u'Wa',
                    u'Walks.ru',
                    u'War',
                    u'WEF',
                    u'Wikimedia relicensing',
                    u'Wikipedia-screenshot',
                    u'Wikiportrait',
                    u'Wikispecies',
                    u'WLA',
                    u'Wo',
                    u'Wulfstan GFDL',
                    u'Wuu',
                    u'Www.bordeaux-port.fr',
                    u'Www.folketinget.dk',
                    u'Www.hotelviewarea.com',
                    u'Www.nordenskirker.dk',
                    u'Xal',
                    u'XGSC image',
                    u'Xh',
                    u'YAM',
                    u'Ydd',
                    u'Yi',
                    u'Yo',
                    u'Yue',
                    u'Za',
                    u'Zea',
                    u'Zh',
                    u'Zh-classical',
                    u'Zh-cn',
                    u'Zh-hans',
                    u'Zh-hant',
                    u'Zh-hk',
                    u'Zh-min-nan',
                    u'Zh-sg',
                    u'Zh-tw'
                    u'Zh-yue',
                    u'Zlib',
                    u'Zu',
                    u'Zxx',]

puttext = u'\n{{Uncategorized|year={{subst:CURRENTYEAR}}|month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}}}'
putcomment = u'Please add categories to this image'

def uploadedYesterday(site = None):
    '''
    Return a pagegenerator containing all the pictures uploaded yesterday.
    Should probably copied to somewhere else
    '''
    result = []
    dateformat ="%Y-%m-%dT00:00:00Z"
    today = datetime.utcnow()
    yesterday = today + timedelta(days=-1)

    for item in site.logpages( number = 5000, mode = 'upload', start = yesterday.strftime(dateformat),
                end = today.strftime(dateformat), newer = True, dump = True):
        result.append(item['title'])
    return pagegenerators.PagesFromTitlesGenerator(result, site)

def recentChanges(site = None, delay=0, block=70):
    '''
    Return a pagegenerator containing all the images edited in a certain timespan.
    The delay is the amount of minutes to wait and the block is the timespan to return images in.
    Should probably be copied to somewhere else
    '''

    result = []
    dateformat ="%Y-%m-%dT%H:%M:%SZ"
    rcstart = datetime.utcnow() + timedelta(minutes=-delay-block)
    rcend = datetime.utcnow() + timedelta(minutes=-delay)

    params = {
        'action'    :'query',
        'list'      :'recentchanges',
        'rcstart'   :rcstart.strftime(dateformat),
        'rcend'     :rcend.strftime(dateformat),
        'rcdir'     :'newer',
        'rcnamespace':'6',
        'rcprop'    :'title',
        'rcshow'    :'!bot',
        'rclimit'   :'5000',
        'rctype'    :'edit|log',
        }

    data = query.GetData(params, site)
    try:
        for item in data['query']['recentchanges']:
            result.append(item['title'])
    except IndexError:
        raise NoPage(u'API Error, nothing found in the APIs')
    except KeyError:
        raise NoPage(u'API Error, nothing found in the APIs')

    return pagegenerators.PagesFromTitlesGenerator(result, site)

def isUncat(page):
    '''
    Do we want to skip this page?

    If we found a category which is not in the ignore list it means
    that the page is categorized so skip the page.
    If we found a template which is in the ignore list, skip the page.
    '''
    pywikibot.output(u'Working on '+ page.title())

    for category in page.categories():
        # Check if it's not a red link category
        if category.exists():
            # If the category is hidden -> ignore it, it's not a topic category
            if not category.isHiddenCategory():
                # Check if it's not a category to ignore
                if category.title() not in ignoreCategories:
                    pywikibot.output(u'Got category ' + category.title())
                    return False

    for templateWithTrail in page.templates():
        #Strip of trailing garbage
        template = templateWithTrail.rstrip('\n').rstrip()
        if template in skipTemplates:
            # Already tagged with a template, skip it
            pywikibot.output(u'Already tagged, skip it')
            return False
        elif template.startswith(u'Int:'):
            pywikibot.output(u'Ignoring internationalization template ' + template)
        elif template in ignoreTemplates:
            # template not relevant for categorization
            pywikibot.output(u'Ignore ' + template)
        else:
            pywikibot.output(u'Not ignoring ' + template)
            return False
    return True

def addUncat(page):
    '''
    Add the uncat template to the page
    '''
    newtext = page.get() + puttext
    pywikibot.showDiff(page.get(), newtext)
    try:
        page.put(newtext, putcomment)
    except pywikibot.EditConflict:
        # Skip this page
        pass
    except pywikibot.LockedPage:
        # Skip this page
        pass
    return

def main(args):
    '''
    Grab a bunch of images and tag them if they are not categorized.
    '''
    generator = None
    genFactory = pagegenerators.GeneratorFactory()

    site = pywikibot.getSite(u'commons', u'commons')
    pywikibot.setSite(site)
    for arg in pywikibot.handleArgs():
        if arg.startswith('-yesterday'):
            generator = uploadedYesterday(site)
        elif arg.startswith('-recentchanges'):
            generator = recentChanges(site=site, delay=120)
        else:
            genFactory.handleArg(arg)
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if not generator:
        pywikibot.output(
          u'You have to specify the generator you want to use for the program!')
    else:
        pregenerator = pagegenerators.PreloadingGenerator(generator)
        for page in pregenerator:
            if page.exists() and (page.namespace() == 6) \
                   and (not page.isRedirectPage()) :
                if isUncat(page):
                    addUncat(page)

if __name__ == "__main__":
    try:
        main(sys.argv[1:])
    finally:
        pywikibot.stopme()
